<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="70%">
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="auto"
    >
      <el-form-item label="物资分类" prop="goodsType">
        <el-select
          v-model="queryParams.goodsType"
          placeholder="请选择物资分类"
          clearable
          filterable
          class="!w-150px"
        >
          <el-option
            v-for="dict in goodsTypeList"
            :key="dict.id"
            :label="dict.goodsType"
            :value="dict.goodsType"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物资名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-150px"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="goodsModel">
        <el-input
          v-model="queryParams.goodsModel"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-150px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mt-20px mb-20px">
      <el-tabs v-model="activeTabName" @tab-click="handleTabClick">
        <el-tab-pane label="全部目录" name="0">&nbsp;</el-tab-pane>
        <el-tab-pane label="常用目录" name="1">&nbsp;</el-tab-pane>
      </el-tabs>
      <el-table
        ref="multipleTableRef"
        :loading="loading"
        :data="list"
        :border="true"
        :stripe="true"
        :show-overflow-tooltip="true"
        height="45vh"
        @select="handleSelect"
        @select-all="handleSelectAll"
      >
        <el-table-column fixed="left" type="selection" align="center" width="40" />
        <el-table-column label="物资分类" align="center" prop="goodsType" />
        <el-table-column label="物资名称" align="center" prop="goodsName" />
        <el-table-column label="规格型号" align="center" prop="goodsModel" />
        <el-table-column label="计量单位" align="center" prop="unit" />
      </el-table>
      <div class="clearfix">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup>
import { GoodsApi } from '@/api/rep/goods'
import { GoodsCollectApi } from '@/api/rep/goodscollect'

/** 选择物资 表单 */
defineOptions({ name: 'SelectGoodsForm' })

const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('选择物资') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用

/** 搜索参数 */
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  goodsType: undefined,
  goodsName: undefined,
  goodsModel: undefined
})
const queryFormRef = ref() // 搜索表单 Ref

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数

/** 选项卡 */
const activeTabName = ref('0')
const handleTabClick = (tab) => {
  activeTabName.value = tab.props.name
  resetQuery()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    let data
    if (activeTabName.value === '0') {
      data = await GoodsApi.getGoodsPage2(queryParams.value)
    } else {
      data = await GoodsCollectApi.getGoodsCollectRespPage(queryParams.value)
    }
    list.value = data.list

    total.value = data.total
  } finally {
    loading.value = false
  }
}

// 获取物资分类列表
const goodsTypeList = ref([])
const getGoodsTypeList = async () => {
  goodsTypeList.value = await GoodsApi.getGoodsListGroup()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

// 表格ref
const multipleTableRef = ref()
// 表格的选中数据
const multipleSelection = ref(null)
/** 表格的全选 */
const handleSelectAll = (selection) => {
  multipleSelection.value = null
  multipleTableRef.value?.clearSelection()
}
/** 表格的单选 */
const handleSelect = (selection, row) => {
  multipleTableRef.value?.clearSelection()
  if (selection.length != 0) {
    multipleSelection.value = row
    multipleTableRef.value?.toggleRowSelection(row, true)
  } else {
    multipleSelection.value = null
    multipleTableRef.value?.toggleRowSelection(row, false)
  }
}

/** 打开弹窗 */
const open = () => {
  dialogVisible.value = true
  resetForm()

  getList()
  getGoodsTypeList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = () => {
  // 判断有没有选择数据
  if (multipleSelection.value == null) {
    return message.warning('请选择数据')
  }

  // 提交请求
  formLoading.value = true
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', multipleSelection.value)
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  // 搜索表单
  queryParams.value = {
    pageNo: 1,
    pageSize: 10,
    goodsType: undefined,
    goodsName: undefined,
    goodsModel: undefined
  }
  queryFormRef.value?.resetFields()

  multipleSelection.value = null
}
</script>

<style lang="scss" scoped>
.clearfix::after {
  content: '';
  display: table;
  clear: both;
}
</style>
